package com.epalmpay.controller.manage.operator;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.epalmpay.commom.BaseController;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.operator.SysLogInDTO;
import com.epalmpay.dto.operator.SysLogListOutDTO;
import com.epalmpay.dto.operator.SysLogOutDTO;
import com.epalmpay.dto.operator.SysLogQueryDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.Syslog;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;

/**
 * Created by lifayi on 2017/5/3.
 */
@RestController
@RequestMapping("/manager/syslog")
public class SysLogController extends BaseController {

    @Autowired
    private ISysLogService sysLogService;

    @RequestMapping(value = "/addOperatorLog", method = RequestMethod.POST)
    public BaseOutDTO addSysLog(@RequestBody SysLogInDTO sysLogInDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
	        int i = sysLogService.addSysLog(sysLogInDTO.getUserId(),sysLogInDTO.getUsername(),sysLogInDTO.getIp(),sysLogInDTO.getContent(),sysLogInDTO.getGroupId());
	        if(i == 1) {
	            baseOutDTO.setErrorCode(Rescode.SUCCESS, "记录添加成功");
	        } else {
	            baseOutDTO.setErrorCode(Rescode.FAIL);
	        }
        } catch (Exception e) {            
            e.printStackTrace();
            logger.error(e);
        }
        return baseOutDTO;
    }

    @RequestMapping(value = "/listSysLog")
    public SysLogListOutDTO listSysLog(SysLogQueryDTO sysLogQueryDTO, PageDTO pageDTO) {
        SysLogListOutDTO sysLogListOutDTO = new SysLogListOutDTO();
        List<SysLogOutDTO> sysLogOutDTOList = new ArrayList<SysLogOutDTO>();

        Page page = Tools.pageEmtity(pageDTO);

        List<Syslog> syslogList = null;
        try {
            syslogList = sysLogService.listSysLog(sysLogQueryDTO, page);
            for (int i = 0; i < syslogList.size(); i++) {
                SysLogOutDTO sysLogOutDTO =new SysLogOutDTO();
                PropertyCopyUtil.copyProperties(syslogList.get(i), sysLogOutDTO);
                sysLogOutDTOList.add(sysLogOutDTO);
            }
            sysLogListOutDTO.setSysLogOutDTOList(sysLogOutDTOList);
            sysLogListOutDTO.setPage(page);
            sysLogListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
        } catch (ParseException e) {
            logger.error("user parameter parse error");
            sysLogListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "服务器处理异常");
        } catch (Exception e) {
            sysLogListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "服务器处理异常");
            e.printStackTrace();
        }
        return sysLogListOutDTO;
    }

    @RequestMapping(value = "/delSysLog")
    public BaseOutDTO delSysLog(@RequestBody SysLogQueryDTO sysLogQueryDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
	        int i = sysLogService.delSysLog(sysLogQueryDTO);
	        if(i > 0) {
	            baseOutDTO.setErrorCode(Rescode.SUCCESS);
	        } else {
	            baseOutDTO.setErrorCode(Rescode.FAIL);
	        }
        } catch (Exception e) {            
            e.printStackTrace();
            logger.error(e);
        }

        return baseOutDTO;
    }
}
